package com.example.xyr.myapplication.view;

import android.app.Activity;
import android.app.FragmentManager;

import com.example.xyr.myapplication.utils.ThreadUtil;


/**
 * 负责dialog的显示
 * 
 * @author honglin
 */
public class DialogDisplay {
	private static DialogDisplay instance = null;
	private boolean touchOutside = true;

	private DialogDisplay() {
	}

	public static DialogDisplay getInstance() {
		synchronized (DialogDisplay.class) {
			if (instance == null) {
				instance = new DialogDisplay();
			}
		}

		return instance;
	}
	


	public DialogDisplay setCanceledOnTouchOutside(boolean touchOutside){
		if (instance == null)getInstance();
		this.touchOutside = touchOutside;
		return instance;
	}
	

	/**
	 * 显示加载框
	 * @param activity
	 * @param msg
     */
	public void dialogLoading(final Activity activity, final String msg) {
		ThreadUtil.getInstance().runMainThread(new Runnable() {

			@Override public void run() {
				if (activity != null) {
					FragmentManager fragmentManager = activity.getFragmentManager();
					if (fragmentManager != null) {
						LoadingDialogFragment loadingDialogFragment = (LoadingDialogFragment) fragmentManager.findFragmentByTag(LoadingDialogFragment.class.getName());
						if (loadingDialogFragment == null) {
							loadingDialogFragment = LoadingDialogFragment.getInstance();
							loadingDialogFragment.showAllowingStateLoss(fragmentManager);
						}
						loadingDialogFragment.setCancelable(true);
						loadingDialogFragment.setMessage(msg);
					}
				}
			}
		});
	}

	/**
	 * 显示加载框
	 * @param activity
	 * @param msg
     */
	public void dialogLoading(final Activity activity, final String msg, final LoadingDialogFragment.LoadingDialogListener listener) {
		ThreadUtil.getInstance().runMainThread(new Runnable() {

			@Override public void run() {
				if (activity != null) {
					FragmentManager fragmentManager = activity.getFragmentManager();
					if (fragmentManager != null) {
						LoadingDialogFragment loadingDialogFragment = (LoadingDialogFragment) fragmentManager.findFragmentByTag(LoadingDialogFragment.class.getName());
						if (loadingDialogFragment == null) {
							loadingDialogFragment = LoadingDialogFragment.getInstance(listener);
							loadingDialogFragment.showAllowingStateLoss(fragmentManager);
						}
						loadingDialogFragment.setCancelable(true);
						loadingDialogFragment.setMessage(msg);
					}
				}
			}
		});
	}

	/**
	 * 显示加载框-并设置点击返回键是否可以关闭
	 * @param activity
	 * @param msg
	 */
	public void dialogLoading(final Activity activity, final String msg, final LoadingDialogFragment.LoadingDialogListener listener, final boolean cancelable) {
		ThreadUtil.getInstance().runMainThread(new Runnable() {

			@Override public void run() {
				if (activity != null) {
					FragmentManager fragmentManager = activity.getFragmentManager();
					if (fragmentManager != null) {
						LoadingDialogFragment loadingDialogFragment = (LoadingDialogFragment) fragmentManager.findFragmentByTag(LoadingDialogFragment.class.getName());
						if (loadingDialogFragment == null) {
							loadingDialogFragment = LoadingDialogFragment.getInstance(listener);
							loadingDialogFragment.showAllowingStateLoss(fragmentManager);
						}
						loadingDialogFragment.setCancelable(cancelable);
						loadingDialogFragment.setMessage(msg);
					}
				}
			}
		});
	}

	public void dialogCloseLoading(final Activity activity) {
		ThreadUtil.getInstance().runMainThread(new Runnable() {

			@Override public void run() {
				if (activity != null) {
					FragmentManager fragmentManager = activity.getFragmentManager();
					if (fragmentManager != null) {
						LoadingDialogFragment loadingDialogFragment = (LoadingDialogFragment) fragmentManager.findFragmentByTag(LoadingDialogFragment.class.getName());
						if (loadingDialogFragment != null) {
							loadingDialogFragment.dismissAllowingStateLoss();
						}
					}
				}
			}
		});

	}
}






